package com.hyf.translucentstatusbar.activity;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyf.translucentstatusbar.MainActivity;
import com.hyf.translucentstatusbar.R;
import com.hyf.translucentstatusbar.app.MyApplication;
import com.hyf.translucentstatusbar.base.BaseActivity;
import com.hyf.translucentstatusbar.util.StatusBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class NavigationActivity extends Base {

    @BindView(R.id.drawer)
    DrawerLayout drawer;
    @BindView(R.id.nav)
    NavigationView nav;
    @BindView(R.id.hyf_dark_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.sb_nav)
    SeekBar mSeekBar;
    @BindView(R.id.tv_nav_result)
    TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_navigation);
        setHeaderBarEnable(false);
        ButterKnife.bind(this);
        mSeekBar.setProgress(MyApplication.STATUS_BAR_ALPHA);
        tvResult.setText(MyApplication.STATUS_BAR_ALPHA + "");
        StatusBarUtils.setTranslucentDrawerLayout(this, drawer, onHeaderBarDrawable(), onHeaderBarStatusViewAlpha());
        initNavigation();
        initNav();
        initListener();
    }

    private void initListener() {
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                MyApplication.STATUS_BAR_ALPHA = progress;
                tvResult.setText(MyApplication.STATUS_BAR_ALPHA + "");
                setHeaderStatusBarAlpha(MyApplication.STATUS_BAR_ALPHA);
                MainActivity.mActivity.setHeaderStatusBarAlpha(MyApplication.STATUS_BAR_ALPHA);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void initNavigation() {
        Drawable drawable = getResources().getDrawable(R.drawable.ic_menu_black_24dp);
        drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
        mToolbar.setNavigationIcon(drawable);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawer.isDrawerOpen(Gravity.LEFT)) {
                    drawer.closeDrawers();
                } else {
                    drawer.openDrawer(Gravity.LEFT);
                }
            }
        });
    }


    private void initNav() {
        mToolbar.setVisibility(View.VISIBLE);
        mToolbar.setTitle("德玛西亚万岁");
        mToolbar.setBackgroundDrawable(onHeaderBarDrawable());
        View view = nav.inflateHeaderView(R.layout.nav_header);
        ImageView headerBg = view.findViewById(R.id.nav_header_top_bg);
        ImageView headerAvatar = view.findViewById(R.id.nav_header_avatar);
        Glide.with(this).load(R.mipmap.nav_header_top_bg)
                .bitmapTransform(new ColorFilterTransformation(this, Color.parseColor("#60333333")))
                .into(headerBg);
        Glide.with(this).load(R.mipmap.avatar)
                .bitmapTransform(new CropCircleTransformation(this))
                .into(headerAvatar);
        // 隐藏NavigationView滑动的滚动条
        setNavigationScrollbarsEnable(nav, false);
    }

    @Override
    protected Drawable onHeaderBarDrawable() {
        int[] colors = {getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorPrimaryLight)};
        return new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
    }

    @Override
    protected boolean isImageHeader() {
        return true;
    }
}
